package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.User;
import com.dao.*;
import com.service.MD5;
import com.service.SendMessage;

/**
 * Servlet implementation class UserEditor
 */
@WebServlet("/UserEdit")
public class UserEdit extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void init(ServletConfig config) throws ServletException {
	}
//找回密码的相关操作
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		String name=request.getParameter("losername");
		String email=request.getParameter("loseremail");
		String action=request.getParameter("action");
		User user=new UserSQL().queryUser(name);
		if("recallPassword".equals(action)){
			request.setAttribute("pass", user);
			request.getRequestDispatcher("resetPass.jsp").forward(request, response);
		}else if("resetPass".equals(action)){//重置密码
			String rpassword=request.getParameter("rpassword");//原来的密码
			if(!user.getPassword().equals(rpassword)||"admin".equals(name))//判断权限,防止恶意修改密码
				response.sendRedirect("login.jsp");
			String password=request.getParameter("password");
			user.setPassword(MD5.stringMD5(password));
			System.out.println(1111111);
			new UserSQL().updateUser(user);
			System.out.println(222222);
			System.out.println();
			response.sendRedirect("userinfo.jsp?msg=OK!");
		}else if(user==null){
			session.setAttribute("state1", "用户名错误");
			response.sendRedirect("recallPass.jsp");
		}else if((user.getEmail()).equals(email)){
		SendMessage sm=new SendMessage();
		String message="<h1>重设密码</h1><p>若您不幸遗忘博客的密码,"
				+ "请<a href=\"http://localhost:8080/isdoub/resetPassword?action=recallPassword&losername="
				+name;
		sm.doSendHtmlEmail("isdoub blog", message, email);
		session.setAttribute("state1", "邮件已发送,注意查收");
		response.sendRedirect("recallPass.jsp");
		}else{
			session.setAttribute("state1", "请输入正确的绑定邮箱");
			response.sendRedirect("recallPass.jsp");
		}
	}
	
//注册验证,注册成功则发邮件通知用户
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		String name=(String)request.getParameter("name");
		String action=request.getParameter("action");
		if("check".equals(action)){
			PrintWriter out=response.getWriter();
			if(new UserSQL().queryUser(name)!=null)
				out.write("false");
			else
				out.write("true");
			return;
		}
		if(new UserSQL().queryUser(name)!=null){
			session.setAttribute("state2", "该用户已存在");
		}else{//新增用户
			String password=MD5.stringMD5(request.getParameter("password"));
			String email=request.getParameter("email");
			String sex=request.getParameter("gender");
			int age=Integer.parseInt(request.getParameter("age"));
			SendMessage sm=new SendMessage();
			sm.doSendHtmlEmail("isdoub blog", "<h1>恭喜您注册博客帐号成功</h1><br/><a href='http://localhost:8080/isdoub/login.jsp'>去看看</a>", email);
		User user=new User(-1,name,password,email,sex,age);
		new UserSQL().storeUser(user);
		session.setAttribute("state1", "注册成功");
		response.sendRedirect("login.jsp");
		return;
		}
		response.sendRedirect("register.jsp");
		
	}

}
